﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MC.RideEvent.Mvc.Helpers.Exceptions;
using MC.RideEvent.Mvc.Models.Repository;
using MC.RideEvent.Mvc.Models.ViewModel;

namespace MC.RideEvent.Mvc.Controllers
{
    public class WayController : ControllerBase
    {
        private IWayRepository _wayRepo;
        private IVehiculeRepository _vehiculeRepo;

        public WayController(IWayRepository wayRepo, IVehiculeRepository vehiculeRepo)
        {
            _wayRepo = wayRepo;
            _vehiculeRepo = vehiculeRepo;
        }

        public ActionResult Index()
        {
            var list = _wayRepo.GetWays();

            return View(list);
        }

        public ActionResult CarPoolingForm()
        {
            return View();
        }

        public ActionResult CarPoolingEditForm(int id)
        {
            var memberId = CurrentMember.Id;
            var model = _wayRepo.GetWayById(id);

            return View(model);
        }

        [HttpPost]
        public ActionResult CarPoolingForm([Bind(Exclude = "Event")]WayViewModel way)
        {
            if(ModelState.IsValid)
            {
                var memberId = CurrentMember.Id;
                way.DriverId = memberId;

                var isUpdate = way.Id != 0;
                try
                {
                    _wayRepo.SaveWay(way, CurrentMember.Id, isUpdate);
                    ViewData["NotificationMsg"] = "Votre trajet a bien été créé.";
                }
                catch (NumberPlaceOutOfBoundException e)
                {
                    ViewData["ValidationMsg"] = e.ValidationMsg;
                }
            }

            return RedirectToAction("Index", "Member");
        }

        //ASUPP SI LA PAGE DETAIL CONTIENT LA LISTE DE  WAYS
        //public ActionResult ListWaysForPassager(int eventId)
        //{
        //    var memberId = CurrentMember.Id;
        //    var list = _wayRepo.GetWaysByEventId(eventId);

        //    return View(list);
        //}

        public ActionResult BecomePassagerForm(int wayId)
        {
            var memberId = CurrentMember.Id;
            var way = _wayRepo.GetWayById(wayId);

            return View(way);
        }

        [HttpPost]
        public ActionResult BecomePassager(int wayId)
        {
            var member = CurrentMember;

            if(true)
            {
                
            }

            return Json(new { success = true, msg = "validation form passager" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// This method redirect on way detail page
        /// with way id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult WayDetail(int Id)
        {
            var way = _wayRepo.GetWay(Id);
            return View(way);
        }

        #region Methods Services Json
        public ActionResult ListLastWaysJson()
        {
            var list = from w in _wayRepo.GetLastWays()
                       select new
                                  {
                                      city = w.City,
                                      place = w.NamePlace,
                                      date = w.DateEvent,
                                      fee = w.Contribution,
                                      numberPlaces = w.NumberPlaces
                                  };

            var result = list.ToList();

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
